<?php
// +---------------------------------------------------------------------+
// | OneBase    | [ WE CAN DO IT JUST THINK ]                            |
// +---------------------------------------------------------------------+
// | Licensed   | http://www.apache.org/licenses/LICENSE-2.0 )           |
// +---------------------------------------------------------------------+
// | Author     | Bigotry <3162875@qq.com>                               |
// +---------------------------------------------------------------------+
// | Repository | https://gitee.com/Bigotry/OneBase                      |
// +---------------------------------------------------------------------+

namespace app\admin\logic;

/**
 * 行为日志逻辑
 */
class Log extends AdminBase
{
    
    /**
     * 获取日志列表
     */
    public function getLogList()
    {
        
        $sub_member_ids = $this->logicMember->getSubMemberIds(MEMBER_ID);
        
        $where = [];
        
        $sub_member_ids[] = MEMBER_ID;
        
        !IS_ROOT && $where['member_id'] = ['in', $sub_member_ids];
        
        return $this->modelActionLog->getList($where, true, 'create_time desc');
    }
  
    /**
     * 日志删除
     */
    public function logDel($where = [])
    {
        
        return $this->modelActionLog->deleteInfo($where) ? [RESULT_SUCCESS, '日志删除成功'] : [RESULT_ERROR, $this->modelActionLog->getError()];
    }
    
    /**
     * 日志添加
     */
    public function logAdd($name = '', $describe = '')
    {
        
        $member_info = session('member_info');
        
        $request = request();
        
        $data['member_id'] = $member_info['id'];
        $data['username']  = $member_info['username'];
        $data['ip']        = $request->ip();
        $data['url']       = $request->url();
        $data['status']    = DATA_NORMAL;
        $data['name']      = $name;
        $data['describe']  = $describe;
        
        $url = url('logList');
        
        return $this->modelActionLog->setInfo($data) ? [RESULT_SUCCESS, '日志添加成功', $url] : [RESULT_ERROR, $this->modelActionLog->getError()];
    }


    /**
     * 获取短信日志列表
     */
    public function getSmsLogList($data)
    {

        $where = [];
        !empty($data['mobile']) && $where['mobile|code|area']=$data['mobile'];
        $where = $where1 = [];
        if (!empty($data['date1']) && !empty($data['date2']) ) {
            $where['a.create_time'] = ['between', [strtotime($data['date1'] . ' 00:00:00'), strtotime($data['date2'] . ' 23:59:59')]];
        }
        $count=$this->modelUserSms->where($where)->count();
        $list=$this->modelUserSms->getList($where, true, 'create_time desc');
        return ['count'=>$count,'list'=>$list];
    }
    /**
     * 获取邮件日志列表
     */
    public function getEmailLogList($data)
    {

        $where = [];
        !empty($data['email']) && $where['email|code']=$data['email'];
        $where = $where1 = [];
        if (!empty($data['date1']) && !empty($data['date2']) ) {
            $where['a.create_time'] = ['between', [strtotime($data['date1'] . ' 00:00:00'), strtotime($data['date2'] . ' 23:59:59')]];
        }
        $count=$this->modelUserEmailLog->where($where)->count();
        $list=$this->modelUserEmailLog->getList($where, true, 'create_time desc');
        return ['count'=>$count,'list'=>$list];
    }
    /**
     * 获取短信日志列表
     */
    public function getBlackLogList($data)
    {

        $where = [];

        !empty($data['type']) && $where['a.type']=$data['type'];
        !empty($data['mobile']) && $where['c.id|c.username|c.mobile']=$data['mobile'];

        if (!empty($data['date1']) && !empty($data['date2']) ) {
            $where['a.create_time'] = ['between', [strtotime($data['date1'] . ' 00:00:00'), strtotime($data['date2'] . ' 23:59:59')]];
        }
        $count=$this->modelUserBlackRecord->alias('a')->join('__MEMBER__ c' ,'a.user_id = c.id')->where($where)->count();
        $list=$this->modelUserBlackRecord->alias('a')->field('a.*,c.username,c.mobile')->join('__MEMBER__ c' ,'a.user_id = c.id')->where($where)->order('a.id desc')->paginate(DB_LIST_ROWS);
        return ['count'=>$count,'list'=>$list];
    }
    /**
     * 获取升级日志列表
     */
    public function getLevelLogList($data)
    {

        $where = [];

        !empty($data['type']) && $where['a.type']=$data['type'];
        !empty($data['mobile']) && $where['c.id|c.username|c.mobile']=$data['mobile'];
        !empty($data['level_id']) && $where['a.level']=$data['level_id'];
        if (!empty($data['date1']) && !empty($data['date2']) ) {
            $where['a.create_time'] = ['between', [strtotime($data['date1'] . ' 00:00:00'), strtotime($data['date2'] . ' 23:59:59')]];
        }
        $count=$this->modelUserLevelLog->alias('a')->join('__MEMBER__ c' ,'a.user_id = c.id')->where($where)->count();
        $list=$this->modelUserLevelLog->alias('a')->field('a.*,c.username,c.mobile')->join('__MEMBER__ c' ,'a.user_id = c.id')->where($where)->order('a.id desc')->paginate(DB_LIST_ROWS);
        return ['count'=>$count,'list'=>$list];
    }

    /**
     * 获取信用评级日志列表
     */
    public function getCreditLogList($data)
    {

        $where = [];

        !empty($data['type']) && $where['a.type']=$data['type'];
        !empty($data['mobile']) && $where['c.id|c.username|c.mobile']=$data['mobile'];
        !empty($data['level_id']) && $where['a.level']=$data['level_id'];
        if (!empty($data['date1']) && !empty($data['date2']) ) {
            $where['a.create_time'] = ['between', [strtotime($data['date1'] . ' 00:00:00'), strtotime($data['date2'] . ' 23:59:59')]];
        }
        $count=$this->modelUserCreditLog->alias('a')->join('__MEMBER__ c' ,'a.user_id = c.id')->where($where)->count();
        $list=$this->modelUserCreditLog->alias('a')->field('a.*,c.username,c.mobile')->join('__MEMBER__ c' ,'a.user_id = c.id')->where($where)->order('a.id desc')->paginate(DB_LIST_ROWS);
        return ['count'=>$count,'list'=>$list];
    }

    /**
     * 获取OTC封禁日志列表
     */
    public function getOtcLogList($data)
    {

        $where = [];

        !empty($data['type']) && $where['a.type']=$data['type'];
        !empty($data['mobile']) && $where['a.user_id|a.username|a.mobile']=$data['mobile'];
        if (!empty($data['date1']) && !empty($data['date2']) ) {
            $where['a.create_time'] = ['between', [strtotime($data['date1'] . ' 00:00:00'), strtotime($data['date2'] . ' 23:59:59')]];
        }
        $count=$this->modelOtcCancelLog->alias('a')->where($where)->count();
        $list=$this->modelOtcCancelLog->alias('a')->field('a.*')->where($where)->order('id desc')->paginate(DB_LIST_ROWS);
//        dd($list);
        return ['count'=>$count,'list'=>$list];
    }

    /**
     * 获取OTC封禁日志列表
     */
    public function getUserNoticeList($data)
    {

        $where = [];

        !empty($data['mobile']) && $where['a.user_id|c.username']=$data['mobile'];
        if (!empty($data['date1']) && !empty($data['date2']) ) {
            $where['a.create_time'] = ['between', [strtotime($data['date1'] . ' 00:00:00'), strtotime($data['date2'] . ' 23:59:59')]];
        }
        $count=$this->modelUserNotice->alias('a')->join('__MEMBER__ c','a.user_id=c.id')->where($where)->count();
        $list=$this->modelUserNotice->alias('a')->field('a.*,c.username')->join('__MEMBER__ c','a.user_id=c.id')->where($where)->order('id desc')->paginate(DB_LIST_ROWS);
//        dd($list);
        return ['count'=>$count,'list'=>$list];
    }
}
